package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.NewsCategory;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.NewsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class NewsCategoryService {

    @Autowired
    private NewsCategoryMapper newsCategoryMapper;


    /**
     * 新增
     * @param newsCategory
     * @return
     */
    public int insert(NewsCategory newsCategory){
        newsCategory.setStatus(BaseStatusEnum.INVALID.getValue());
        newsCategoryMapper.insert(newsCategory);
        return newsCategory.getId();
    }


    /**
     * 更新
     * @param newsCategory
     * @return
     */
    public int update(NewsCategory newsCategory){
        if(newsCategory.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        checkCategoryLevel(newsCategory);
        return newsCategoryMapper.updateByPrimaryKeySelective(newsCategory);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return newsCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public NewsCategory selectById(Integer id){
        return newsCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有分类
     * @return
     */
    public List<NewsCategory> selectAll(){
        return newsCategoryMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     * @param category 名称
     * @param pid 父级id
     * @param status 状态
     * @return
     */
    public List<NewsCategory> selectByParams(String category, Integer pid,Integer status){
        return newsCategoryMapper.selectByParams(category, pid, status);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return newsCategoryMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return newsCategoryMapper.batchDelete(dto.getIds());
    }

    /**
     * 校验层级
     * @param category
     */
    public void checkCategoryLevel(NewsCategory category){
        List<NewsCategory> categories = selectAll();
        List<NewsCategory> son = findCategoryIdByPid(category.getId(), categories);
        List<Integer> ids = son.stream().map(e->e.getId()).collect(Collectors.toList());
        if(ids.contains(category.getPid()) || category.getId().equals(category.getPid())){
            throw new CustomException("当前分类的父类不能为它的子分类和它本身");
        }
    }

    /**
     * 根据父级id所有子级分类id
     * @param pid
     * @param categories
     * @return
     */
    public List<NewsCategory> findCategoryIdByPid(Integer pid, List<NewsCategory> categories){
        List<NewsCategory> list = new ArrayList<>();
        categories.stream().filter(e-> pid.equals(e.getPid())).forEach(item->{
            list.add(item);
            List<NewsCategory> son = findCategoryIdByPid(item.getId(),categories);
            list.addAll(son);
        });
        return list;
    }
}
